home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / serial / modemsta.000 / modemsta / modemstat.c < prev    next >
C/C++ Source or Header  |  1995-09-20  |  5KB  |  195 lines

  1. /******************************************************************
  2.  
  3.  Modem Status lights for Linux console
  4.  Public Domain 1995 by Doug McLean (madsci@mi.net)
  5.  
  6.  This simple program uses ANSI to put a serial status bar at the
  7.  top of a Linux terminal. It is very simple and uses ANSI rather
  8.  than curses.
  9.  
  10. ******************************************************************/ 
  11.  
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <fcntl.h>
  15. #include <string.h>
  16. #include <sys/ioctl.h>
  17.    
  18. #define RED 1
  19. #define GRN 2
  20. #define YEL 3
  21. #define BLU 4
  22. #define PUR 5
  23. #define CYN 6
  24. #define WHT 7
  25. #define BLK 0
  26.  
  27. #define RESET      "\033[0m"
  28. #define BOLD       "\033[1m"
  29. #define SAVE       "\033[s"
  30. #define RESTORE    "\033[u"
  31. #define CLR2TOP    "\033[1J"
  32.  
  33. void          set_col(char,char);
  34. void          position(int,int);
  35. void          usage(char *);
  36. void          open_my_device();
  37.  
  38. char          rts_c,cts_c,dsr_c,cd_c,dtr_c,ring_c;
  39. char          device[255];
  40. char          dispdev[128];
  41. char          *tbuff;
  42. char          dtitle;
  43. int           fh;
  44. unsigned int  status;
  45. unsigned int  ioret;
  46. pid_t         me;
  47.  
  48. #define       RMARG  71
  49.  
  50. char          *version = "\nmodemstat v0.2 public domain 1995 by Doug McLean (madsci@mi.net)\n";
  51.  
  52. void main(int argc, char *argv[])
  53. {
  54.     puts (version);
  55.         if (argc > 2)    usage   (argv[0]);
  56.         if (argc != 2)   strcpy  (device,"/dev/modem");
  57.         else             strcpy  (device,argv[1]);
  58.  
  59.  
  60.         switch(fork())
  61.         {
  62.            case 0:
  63.               setsid();
  64.               break;
  65.               
  66.            case -1:
  67.               printf("%s: can't fork.\n", argv[0]);
  68.               exit(1);
  69.               
  70.            default:
  71.               exit(0);
  72.         }
  73.  
  74.         tbuff = device;
  75.         while ((tbuff = strchr(tbuff,'/'))!=NULL) strcpy (dispdev,++tbuff);
  76.         dispdev[8]=0x00;
  77.  
  78.          open_my_device();
  79.          me = getpid();
  80.          dtitle = 0;
  81.  
  82.         for (;;)
  83.         {
  84.  
  85.                 status = ioctl (fh, TIOCMGET, &ioret);
  86.  
  87.             if (status == 0)
  88.             {
  89.                if (ioret & TIOCM_RTS) rts_c  = GRN;
  90.                                  else rts_c  = RED;
  91.                if (ioret & TIOCM_CTS) cts_c  = GRN;
  92.                                  else cts_c  = RED;
  93.                if (ioret & TIOCM_DSR) dsr_c  = GRN;
  94.                                  else dsr_c  = RED;
  95.                if (ioret & TIOCM_CAR) cd_c   = GRN;
  96.                                  else cd_c   = RED;
  97.                if (ioret & TIOCM_DTR) dtr_c  = GRN;
  98.                                  else dtr_c  = RED;
  99.                if (ioret & TIOCM_RNG) ring_c = GRN;
  100.                                  else ring_c = RED;
  101.             }
  102.             else
  103.             {
  104.                close(fh);
  105.                rts_c   = YEL;
  106.                cts_c   = YEL;
  107.                dsr_c   = YEL;
  108.                cd_c    = YEL;
  109.                dtr_c   = YEL;
  110.                ring_c  = YEL;
  111.                open_my_device();
  112.             }
  113.  
  114.             printf   (SAVE);
  115.                 printf   (BOLD);
  116.  
  117.             position (1,RMARG);
  118.             
  119.             if (dtitle == 0)
  120.             {
  121.                   set_col  (CYN,BLU);
  122.                       printf   (" %-8s ",dispdev);
  123.                       dtitle += 1;
  124.                 }
  125.                 else if (dtitle == 1)
  126.                 {
  127.                       set_col  (CYN,BLU);
  128.                       printf   (" PID%5d ",me);
  129.                       dtitle += 1;
  130.                 }
  131.                 else
  132.                 {
  133.                       if (ioret & TIOCM_CAR)
  134.                       {
  135.                            set_col  (WHT,BLU);
  136.                            printf   ("  ONLINE  ");
  137.                       }
  138.                       else
  139.                       {
  140.                            set_col  (YEL,BLU);
  141.                            printf   (" OFFLINE  ");
  142.                       }
  143.                       dtitle = 0;
  144.                 }
  145.  
  146.                 position (2,RMARG);
  147.             set_col  (rts_c,BLU);   printf (" RTS  ");
  148.             set_col  (cts_c,BLU);   printf ("CTS ");
  149.  
  150.             position (3,RMARG);
  151.             set_col  (dsr_c,BLU);   printf (" DSR  ");
  152.             set_col  (dtr_c,BLU);   printf ("DTR ");
  153.  
  154.             position (4,RMARG);
  155.             set_col  (cd_c,BLU);    printf (" CD   ");
  156.             set_col  (ring_c,BLU);  printf ("RNG ");
  157.  
  158.                 
  159.                 printf   (RESTORE);
  160.                 printf   (RESET);
  161.                 set_col  (WHT,BLK);
  162.                 fflush   (stdout);
  163.                 sleep    (1);
  164.         }
  165. }
  166.  
  167.  
  168. void usage(char *who)
  169. {
  170.     printf ("USAGE: %s [/dev/serial_device_name]\n",who);
  171.     printf ("       Default device is /dev/modem\n");
  172.  
  173. }
  174.  
  175. void set_col(char fg_color,char bg_color)
  176. {
  177.    fg_color += 30; bg_color += 40;
  178.    printf ("\033[%d;%dm",fg_color,bg_color);
  179. }
  180.  
  181. void position (int line,int col)
  182. {
  183.    printf ("\033[%u;%uf",line,col);
  184. }
  185.  
  186. void open_my_device()
  187. {
  188.    fh =  open (device,O_RDONLY);
  189.    if    (fh == -1)
  190.    {
  191.          printf ("\nCannot open %s\n",device);
  192.          exit (1);
  193.    }
  194. }
  195.